package cn.ydxiaoshuai.tools.controller;

import cn.hutool.core.util.CharsetUtil;
import cn.ydxiaoshuai.tools.base.ApiRestController;
import cn.ydxiaoshuai.tools.util.RedisUtil;
import cn.ydxiaoshuai.tools.vo.RedisInfoBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.List;

import cn.ydxiaoshuai.tools.util.oConvertUtils;
/**
 * @author 小帅丶
 * @Description Redis-常量修改
 * @className RedisRestController
 * @Date 2020年9月21日16:43:18
 **/
@Scope("prototype")
@Slf4j
@RestController
@RequestMapping("/rest/redis")
public class RedisRestController extends ApiRestController {
    @Autowired
    private RedisUtil redisUtil;

    /**
     * @param key_name key的名称
     * @param key_value 要更新的key的值
     * @return org.springframework.http.ResponseEntity<java.lang.Object>
     * @Description 修改一些变量
     * @Author 小帅丶
     * @Date 2019年10月23日
     **/
    @RequestMapping(value = {"/update/{key_name}/{key_value}","/update"}, method = {RequestMethod.GET})
    public ResponseEntity<Object> getRotatyList(@PathVariable(required = false) String key_name, @PathVariable(required = false) String key_value, HttpServletRequest request, HttpServletResponse response) {
        RedisInfoBean bean = new RedisInfoBean();
        try {
            if(oConvertUtils.isEmpty(key_name)||oConvertUtils.isEmpty(key_value)){
                bean.fail("not enough param","参数缺失,请检查", 410101);
            } else {
                boolean hasKey = redisUtil.hasKey(key_name);
                if(hasKey){
                    key_value = URLDecoder.decode(key_value, CharsetUtil.UTF_8);
                    boolean updateStatus = redisUtil.set(key_name, key_value);
                    RedisInfoBean.Data data = new RedisInfoBean.Data();
                    data.setKey_name(key_name);
                    data.setKey_value(key_value);
                    data.setUpdate_status(updateStatus);
                    bean.success("success","操作完成",data);
                } else {
                    bean.fail("key does not exist, please check","key不存在,请检查", 410103);
                }
            }
        } catch (Exception e) {
            bean.error("system error", "系统错误");
        }
        return new ResponseEntity<Object>(bean, httpHeaders, HttpStatus.OK);
    }

    /**
     * @Description 获取list
     * @param key_name key的名称
     * @param start 开始位置
     * @param end 结束位置
     * @return org.springframework.http.ResponseEntity<java.lang.Object>
     * @Author 小帅丶
     * @Date 2019年10月23日
     **/
    @RequestMapping(value = {"/lget/{key_name}/{start}/{end}","/lget"}, method = {RequestMethod.GET})
    public ResponseEntity<Object> getLKeyList(@PathVariable(required = false) String key_name,
                                              @PathVariable(required = false) long start,
                                              @PathVariable(required = false) long end) {
        RedisInfoBean bean = new RedisInfoBean();
        try {
            if(oConvertUtils.isEmpty(key_name)||oConvertUtils.isEmpty(start)||oConvertUtils.isEmpty(end)){
                bean.fail("not enough param","参数缺失,请检查", 410101);
            } else {
                boolean hasKey = redisUtil.hasKey(key_name);
                if(hasKey){
                    List<Object> objects = redisUtil.lGet(key_name, start, end);
                    bean.success("success","操作完成",objects);
                } else {
                    bean.fail("key does not exist, please check","key不存在,请检查", 410103);
                }
            }
        } catch (Exception e) {
            bean.error("system error", "系统错误");
        }
        return new ResponseEntity<Object>(bean, httpHeaders, HttpStatus.OK);
    }

    /**
     * @Description 获取key的内容
     * @param key_name key的名称
     * @return org.springframework.http.ResponseEntity<java.lang.Object>
     * @Author 小帅丶
     * @Date 2019年10月23日
     **/
    @RequestMapping(value = {"/get/{key_name}","/get"}, method = {RequestMethod.GET})
    public ResponseEntity<Object> getLKeyList(@PathVariable(required = false) String key_name) {
        RedisInfoBean bean = new RedisInfoBean();
        try {
            if(oConvertUtils.isEmpty(key_name)){
                bean.fail("not enough param","参数缺失,请检查", 410101);
            } else {
                boolean hasKey = redisUtil.hasKey(key_name);
                if(hasKey){
                    String values = redisUtil.get(key_name).toString();
                    RedisInfoBean.Data data = new RedisInfoBean.Data();
                    data.setKey_name(key_name);
                    data.setKey_value(values);
                    bean.success("success","操作完成",data);
                } else {
                    bean.fail("key does not exist, please check","key不存在,请检查", 410103);
                }
            }
        } catch (Exception e) {
            bean.error("system error", "系统错误");
        }
        return new ResponseEntity<Object>(bean, httpHeaders, HttpStatus.OK);
    }

}
